// 云对象教程: https://uniapp.dcloud.net.cn/uniCloud/cloud-obj
// jsdoc语法提示教程：https://ask.dcloud.net.cn/docs/#//ask.dcloud.net.cn/article/129
module.exports = {
  _before: function () {
    // 通用预处理器
  },
  //获取壁纸列表
  async list({ pageSize = 9, pageNum = 1, classid = '' } = {}) {
    if (!classid)
      return {
        errCode: 400,
        errMsg: '分类id不能为空',
      };
    pageSize = Math.min(pageSize, 20);
    let skip = (pageNum - 1) * pageSize;
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });

    let picTemp = dbJQL
      .collection('dc-bizhi-piclist')
      .where(`classid == '${classid}' && checked == true`)
      .orderBy('createTime', 'desc')
      .skip(skip)
      .limit(pageSize)
      .getTemp();

    let userTemp = dbJQL
      .collection('uni-id-users')
      .field('_id,nickname')
      .getTemp();

    let classTemp = dbJQL
      .collection('dc-bizhi-classify')
      .field('_id,name')
      .getTemp();

    let res = await dbJQL
      .collection(picTemp, userTemp, classTemp)
      .field(
        `
		_id,
		checked,
		classid.name as classname,
    classid._id as classid,
		description,
		picurl,
		score,
		sort,
		tabs,
		user_id.nickname as nickname,
		view_count
		`
      )
      .get();

    let data = res.data.map((item) => {
      return {
        ...item,
        classname: item.classname[0],
        nickname: item.nickname[0],
        classid: item.classid[0],
      };
    });

    return { ...res, data };
  },
  // 获取一条壁纸
  async item(id = null) {
    if (!id)
      return {
        errCode: 400,
        errMsg: 'id不能为空',
      };

    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });

    let picTemp = dbJQL
      .collection('dc-bizhi-piclist')
      .where(`_id == '${id}' && checked == true`)
      .getTemp();

    let userTemp = dbJQL
      .collection('uni-id-users')
      .field('_id,nickname')
      .getTemp();

    let classTemp = dbJQL
      .collection('dc-bizhi-classify')
      .field('_id,name')
      .getTemp();

    let res = await dbJQL
      .collection(picTemp, userTemp, classTemp)
      .field(
        `
		_id,
		checked,
		classid.name as classname,
    classid._id as classid,
		description,
		picurl,
		score,
		sort,
		tabs,
		user_id.nickname as nickname,
		view_count
		`
      )
      .get({ getOne: true });

    let data = {
      ...res.data,
      classname: res.data.classname[0],
      nickname: res.data.nickname[0],
      classid: res.data.classid[0],
    };

    return { ...res, data };
  },
  //增加阅读量
  async addRead(id = null) {
    if (!id)
      return {
        errCode: 400,
        errMsg: 'id不能为空',
      };

    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });

    let res = await dbJQL
      .collection('dc-bizhi-piclist')
      .doc(id)
      .get({ getOne: true });
    let count = (res.data.view_count += 10);
    dbJQL.collection('dc-bizhi-piclist').doc(id).update({
      view_count: count,
    });
  },
  //搜索
  async search({ pageSize = 9, pageNum = 1, keyword = '' } = {}) {
    if (!keyword)
      return {
        errCode: 400,
        errMsg: '关键词不能为空',
      };
    pageSize = Math.min(pageSize, 20);
    let skip = (pageNum - 1) * pageSize;
    let wre = {
      checked: true,
      $or: [
        { description: { $regex: keyword, $options: 'i' } },
        { tabs: { $regex: keyword, $options: 'i' }},
      ],
    };
    const dbJQL = uniCloud.databaseForJQL({
      clientInfo: this.getClientInfo(),
    });

    let picTemp = dbJQL
      .collection('dc-bizhi-piclist')
      .where(wre)
      .orderBy('createTime', 'desc')
      .skip(skip)
      .limit(pageSize)
      .getTemp();

    let userTemp = dbJQL
      .collection('uni-id-users')
      .field('_id,nickname')
      .getTemp();

    let classTemp = dbJQL
      .collection('dc-bizhi-classify')
      .field('_id,name')
      .getTemp();

    let res = await dbJQL
      .collection(picTemp, userTemp, classTemp)
      .field(
        `_id,
			checked,
			classid.name as classname,
			description,
			picurl,
			score,
			sort,
			tabs,
			user_id.nickname as nickname,
			view_count
			`
      )
      .get({ getCount: true });

    let data = res.data.map((item) => {
      return {
        ...item,
        classname: item.classname[0],
        nickname: item.nickname[0],
      };
    });

    return { ...res, data };
  },
};
